
import { _decorator, Component, Node, Label } from 'cc';
import { main } from '../../script/main';
import { Message } from '../../script/Message';
import { ModuleBase } from '../../script/ModuleBase';
import { ModuleCenter } from '../../script/ModuleCenter';
import { Actor } from '../Actor/Actor';
import { RoleBase } from '../Actor/RoleBase';
const { ccclass, property } = _decorator;



@ccclass('View')
export class View extends ModuleBase {
    @property(Node)
    public Label: Node = null;
    @property(Node)
    public rim1: Node = null;
    @property(Node)
    public rim2: Node = null;
    @property(Node)
    public rim3: Node = null;

    public rim1RoleName: string = null;
    public rim2RoleName: string = null;
    public rim3RoleName: string = null;
    onLoad() {
        this.init().then(()=>{
            main.getInstance().offLoading();
        });
    }

    async init(){
        this.moduleName = "View";     
        this.addModule(this);
        //记载Actor模块
        let actorNode = await main.getInstance().getModuleNode("Actor");
        actorNode.setParent(this.node);
        this.rim1.on(Node.EventType.TOUCH_START, this.rim1End, this);
        this.rim2.on(Node.EventType.TOUCH_START, this.rim2End, this);
        this.rim3.on(Node.EventType.TOUCH_START, this.rim3End, this);
    }

    async rim1End() {
        let role = await this.setRole(this.rim1);
        this.rim1RoleName = role.actorName;
    }
    async rim2End() {
        let role = await this.setRole(this.rim2)
        this.rim2RoleName = role.actorName;
    }
    async rim3End() {
        let role = await this.setRole(this.rim3)
        this.rim3RoleName = role.actorName;
    }

    setRole(area: Node): Promise<RoleBase> {
        area.children[0].destroy();
        const roleActor: Actor = ModuleCenter.getModule("Actor") as Actor;
        return roleActor.getRandomRole(area);
        
    }

    ReceiveMessage(message: Message) {

    }

    //进入战斗按钮
    buttonEnd() {       
        if (this.rim1RoleName === null || this.rim2RoleName === null || this.rim3RoleName === null) {
            if (this.Label.active === false) {
                this.Label.active = true;
                setTimeout(() => {
                    this.Label.active = false;
                }, 1000);
            }
        } else {
            main.roleList.push(this.rim1RoleName,this.rim2RoleName,this.rim3RoleName);
            main.getInstance().loadScene("pve");
        }
    }


    onDestroy(){
        this.closeModule(this.moduleName);
    }
}

